﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Check_Printing_Solution
{
    public class PrintCoverEngine
    {
        PrintManager manager = new PrintManager();
        TemplateLayout _template = new TemplateLayout();
        Bank _currentbank = new Bank();
        Customer _currentcustomer = new Customer();
        int _coversprinted = 0;
        string _coverpage_nextchequenumber = "";
        string _lastchequenumber = "";
        int _incrementer = 0;
        int _offsetx = 0;
        int _offsety = 0;

        private void PrintCover( string printername )
        {
            PrintDocument template = new PrintDocument();
            PrintController printcontroller = new StandardPrintController();
            template.PrintController = printcontroller;

            template.PrintPage += new PrintPageEventHandler( covertemplate_PrintPage );
            template.DefaultPageSettings.PaperSize = new PaperSize( "Label Paper", _template.PaperWidth, _template.PaperHeight );
            template.PrinterSettings.PrinterName = printername;
            PaperSource papersource = new PaperSource();
            papersource.SourceName = _template.Tray;
            template.DefaultPageSettings.PaperSource = papersource;

            if ( _template.Landscape )
            {
                template.DefaultPageSettings.Landscape = true;
            }
            else
            {
                template.DefaultPageSettings.Landscape = false;
            }

            PrintPreviewDialog printpreviewdialog = new PrintPreviewDialog();
            printpreviewdialog.Document = template;
            template.Print();
        }

        void covertemplate_PrintPage( object sender, PrintPageEventArgs e )
        {
            int padding = 0;

            for ( int i = 0; i < Convert.ToInt32( _template.ChquesPerPage ); i++ )
            {
                FontStyle fntstyle = FontStyle.Regular;

                if ( _template.BoldNameCover == true )
                {
                    fntstyle = FontStyle.Bold;
                }
                else
                {
                    fntstyle = FontStyle.Regular;
                }

                int seqnumber = 0;
                seqnumber = Convert.ToInt32( _lastchequenumber );

                e.Graphics.DrawString( _currentbank.BankAddress, new Font( _template.CoverAddress4Font, Convert.ToSingle( _template.CoverAddress4Size ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverAddress4X ) + _offsetx, Convert.ToInt32( _template.CoverAddress4Y ) + _offsety + padding ) );
                //Name
                e.Graphics.DrawString( _currentcustomer.FirstName + " " + _currentcustomer.LastName, new Font( _template.CoverNameFont, Convert.ToSingle( _template.CoverNameSize ), FontStyle.Bold ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverNameX ) + _offsetx, Convert.ToInt32( _template.CoverNameY ) + _offsety + padding ) );
                //Address1
                e.Graphics.DrawString( _currentcustomer.Line2, new Font( _template.CoverAddress1Font, Convert.ToSingle( _template.CoverAddress1Size ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverAddress1X ) + _offsetx, Convert.ToInt32( _template.CoverAddress1Y ) + _offsety + padding ) );
                //Address2
                e.Graphics.DrawString( _currentcustomer.Line3, new Font( _template.CoverAddress2Font, Convert.ToSingle( _template.CoverAddress2Size ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverAddress2X ) + _offsetx, Convert.ToInt32( _template.CoverAddress2Y ) + _offsety + padding ) );
                //Phone
                e.Graphics.DrawString( _currentcustomer.Line1, new Font( _template.CoverTelephoneFont, Convert.ToSingle( _template.CoverTelephoneSize ), fntstyle ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverTelephoneX ) + _offsetx, Convert.ToInt32( _template.CoverTelephoneY ) + _offsety + padding ) );
                //Next Cheque #
                e.Graphics.DrawString( FormatSeqNumber( seqnumber.ToString() ), new Font( _template.CoverCheckCountFont, Convert.ToSingle( _template.CoverCheckCountSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverCheckCountX ) + _offsetx, Convert.ToInt32( _template.CoverCheckCountY ) + _offsety + padding ) );
                //CoverCheckCount

                string checkno = string.Format( "C{0}C A{1}D{2}A {3}C  {4}", FormatSeqNumber( seqnumber.ToString() ), _currentbank.BranchPrefix, _currentbank.BranchCode, _currentcustomer.ChequeNumber, _template.DepositSlips );
                e.Graphics.DrawString( checkno, new Font( _template.CoverCheckNoFont, Convert.ToSingle( _template.CoverCheckNoSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverCheckNoX ) + _offsetx, Convert.ToInt32( _template.CoverCheckNoY ) + _offsety + padding ) );

                _coverpage_nextchequenumber = ( Convert.ToInt32( _coverpage_nextchequenumber ) + Convert.ToInt32( _template.NoChequesPerBook ) ).ToString();
                padding = padding + Convert.ToInt32( _template.DistanceBetweenCheques );
                _coversprinted++;
            }
        }

        public void InitiatePrint( string printername, Customer customer, Bank bank, string style,
            int totalchequesforprint, string comment, string nextchequenumber, string styleid, int offsetx, int offsety )
        {
            TemplateLayout template = manager.GetTemplateLayout( style, styleid );
            _template = template;
            _currentcustomer = customer;
            _currentbank = bank;
            _coverpage_nextchequenumber = ( totalchequesforprint / Convert.ToInt32( _template.ChquesPerPage ) ).ToString();
            _lastchequenumber = nextchequenumber;
            _offsetx = offsetx;
            _offsety = offsety;

            string bankcode = bank.BranchPrefix;
            _currentbank = manager.GetBankByBankCode( bankcode );

            int pagesperbook = 0;
            int numbooks = 0;
            int remainder = 0;

            pagesperbook = Convert.ToInt32( template.NoChequesPerBook );
            if ( totalchequesforprint > pagesperbook )
            {
                numbooks = totalchequesforprint / Convert.ToInt32( template.ChquesPerPage );
                numbooks = numbooks / Convert.ToInt32( template.NoChequesPerBook );
                remainder = totalchequesforprint - ( numbooks * Convert.ToInt32( template.NoChequesPerBook ) * Convert.ToInt32( template.ChquesPerPage ) );
            }
            else if ( pagesperbook > totalchequesforprint )
            {
                remainder = totalchequesforprint;
            }
            else
            {
                numbooks = 1;
            }

            _incrementer = Convert.ToInt32( template.NoChequesPerBook );

            for ( int i = 0; i < _template.CoverCount; i++ )
            {
                PrintCover( printername );
            }
        }

        private string FormatSeqNumber( string seqnumber )
        {
            int templaterequired = Convert.ToInt32( _template.SeqNumberSize );
            int currentlength = seqnumber.ToString().Count();
            int gap = templaterequired - currentlength;
            string holder = "";

            for ( int i = 0; i < gap; i++ )
            {
                holder = holder + "0";
            }
            holder = holder + seqnumber;

            return holder;
        }
    }
}
